import matplotlib

import numpy
from matplotlib import pyplot


class GetProblemTypePie:
    def __init__(self, fig):
        self.labels = ['用例问题', '环境问题', '其他用例影响', 'bug', '研发代码变更']
        self.colors = ['#70ad47', '#ed7d31', '#ffc000', 'r', 'gray', '#F0E68C']
        self.X = [200, 5, 50, 3, 6]
        self.explode = (0, 0, 0, 0.2, 0, 0)
        self.fig = fig

    def draw_pie(self):
        matplotlib.rcParams['font.family'] = 'STFangSong'
        matplotlib.rcParams['font.size'] = 5
        pyplot.rcParams['axes.unicode_minus'] = False
        pyplot.pie(self.X,
                   labels=self.labels,
                   autopct='%1.2f%%',
                   pctdistance=0.8,
                   startangle=180,
                   colors=self.colors,
                   radius=1.2,
                   counterclock=False,
                   wedgeprops={'edgecolor': 'black',
                               'linestyle': '-',
                               'alpha': 0.5})

        pyplot.title('自动化连跑失败类别占比饼图')

        pyplot.savefig('../static/images/pie.png')
        pyplot.draw()
        pyplot.pause(3)


class GetSuccessRateLineChart:
    def __init__(self, fig):
        self.X = ['2021/5/17', '2021/5/18', '2021/5/19', '2021/5/20',
                  '2021/5/21', '2021/5/22', '2021/5/23']
        self.Y = [83.1, 97.7, 90.01, 99.2, 99.3, 99.5, 99.78]
        self.fig = fig

    def draw_line_chart(self):
        matplotlib.rcParams['font.family'] = 'STFangSong'
        pyplot.rcParams['axes.unicode_minus'] = False

        pyplot.plot(self.X, self.Y, 'g.-', label='成功率(%)', linewidth=1)
        for a, b in zip(self.X, self.Y):
            pyplot.text(a, b, str(b) + '%', ha='center', va='bottom')
        pyplot.xlabel('日期')
        pyplot.ylabel('成功率（%）')
        pyplot.xticks(self.X)
        pyplot.yticks(numpy.arange(0, 130, 10))
        pyplot.legend()
        pyplot.title('最近七日自动化成功率折线图')
        pyplot.savefig('../static/images/lineChart.png')
        pyplot.draw()
        pyplot.pause(3)


if __name__ == '__main__':
    # 交互式绘图，非阻塞式绘图
    pyplot.ion()

    pie_fig = pyplot.figure(figsize=(3, 2), dpi=200)
    my_pie = GetProblemTypePie(pie_fig)
    my_pie.draw_pie()
    pyplot.show()

    line_chart_fig = pyplot.figure(figsize=(3, 2), dpi=200)
    my_line = GetSuccessRateLineChart(line_chart_fig)
    my_line.draw_line_chart()
    pyplot.show()

    pyplot.close('all')
